//
//  StatusViewModel.swift
//  SwiftWB
//
//  Created by 远成物流 on 2017/6/28.
//
//

import UIKit

class StatusViewModel: NSObject {
    // MARK:- 定义属性
    var status : Status?
    var cellHeight : CGFloat = 0
    
    // MARK:- 对数据处理的属性
    var sourceText : String?            // 处理来源
    var createAtText : String?          // 处理创建时间
    var verifiedImage : UIImage?        // 处理用户认证图标
    var vipImage : UIImage?             // 处理用户会员等级
    var profileURL : URL?             // 处理用户头像的地址
    var picURLs : [URL] = [URL]()   // 处理微博配图的数据
    
    init(status:Status){
        self.status = status
        
        //来源
        if let source = status.source , source != "" {
            let startIndex = (source as NSString).range(of: ">").location+1
            let length = (source as NSString).range(of: "</").location-startIndex
            
            sourceText = (source as NSString).substring(with: NSMakeRange(startIndex, length))
        }
        
        //时间
        if let createAt = status.created_at {
            createAtText = Date.createDateString(createAtStr: createAt)
        }
        
        
        //认证
        let verifiedType = status.user?.verified_type ?? -1
        switch verifiedType {
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        
        //会员
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank < 6 {
            vipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        //头像
        let profileUrlString = status.user?.profile_image_url ?? ""
        profileURL = URL(string: profileUrlString)
        
        //配图
        let picUrlsDicts = status.pic_urls?.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls
        if let picUrlsDicts = picUrlsDicts {
            for picUrlsDict in picUrlsDicts{
                guard let picUrlString = picUrlsDict["thumbnail_pic"] else {
                    continue
                }
                
                picURLs.append(URL(string:picUrlString)!)
            }
        }
    }
}
